package com.jf.config;

import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jf.core.Jbsf;
import com.jf.core.WbcsHandler;
import com.jf.plugin.activerecord.ActiveRecordPlugin;
import com.jf.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jf.plugin.activerecord.dialect.MysqlDialect;
import com.jf.plugin.druid.DruidPlugin;
import com.jf.plugin.druid.DruidStatViewHandler;
import com.jf.plugin.wbcs.WbcsPlugin;
import com.jf.render.FreeMarkerRender;
import com.jf.render.ViewType;
import com.jf.template.Engine;

/**
 * config jbsf framework
 * @author hawkfly
 */
public class DefaultConfig extends JbsfConfig {
	
	public static Properties property;
	public static WbcsHandler wbcsHandler;
	
	Log log = LogFactory.getLog(DefaultConfig.class);
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		me.setDevMode(true);
		//me.setBaseViewPath("/webresources/pages/");
		me.setViewType(ViewType.JF_TEMPLATE);
		me.setEncoding("UTF-8");
		property = loadPropertyFile("/conf/set.properties");
		try 
		{
			FreeMarkerRender.getConfiguration().setSharedVariable("basepath", Jbsf.me().getContextPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		DruidStatViewHandler druidStatViewHandler = new DruidStatViewHandler("/druid");
		me.add(druidStatViewHandler);
		
		WbcsHandler wbcsHandler = new WbcsHandler();
		DefaultConfig.wbcsHandler = wbcsHandler;
		me.add(wbcsHandler);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		String url = getProperty("jdbc.url"), usrname = getProperty("jdbc.user"), password = getProperty("jdbc.password"), driver = getProperty("jdbc.driver");
		log.info(url + ", " + usrname + ", " + password + ", " + driver);
		DruidPlugin druidPlugin = new DruidPlugin(url, usrname, password, driver);
		druidPlugin.setValidationQuery("select 1 from dual");
		me.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setDialect(new MysqlDialect());
		arp.setShowSql(true);
		arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		me.add(arp);
		
		WbcsPlugin wbcsPlugin = new WbcsPlugin();
		me.add(wbcsPlugin);
		
		//MpIndex.mapping(arp);//首页
		//MpAuth.mapping(arp);
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		
	}
}
